<template>
  <a-modal
    :title="title"
    :width="640"
    :maskClosable = "false"
    :visible="visible"
    :confirmLoading="loading"
    @ok="ok"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="layout">
        <a-form-item v-show="false" label="">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="面试地点">
          <a-input
                  v-decorator="['arrangedPlace', { rules: [{ required: true, message: '请输入面试地点' }] }]"
          />
        </a-form-item>
        <a-form-item label="面试日期">
          <a-date-picker class="full-item" v-decorator="['date', { rules: [{ required: true, message: '请选择面试日期' }] }]" />
        </a-form-item>
        <a-form-item label="面试时间">
          <a-time-picker format="HH:mm" :minute-step="5" class="full-item"
                         v-decorator="['time', { rules: [{ required: true, message: '请选择面试时间' }] }]" />
        </a-form-item>

        <a-form-item label="面试官">
          <a-select mode="multiple" placeholder="请选择" :allowClear="true"
                    v-decorator="['members', { rules: [{ required: true, message: '请选择面试官' }] }]">
            <a-select-option v-for="(item, index) in members" :key="index"
                             :value="item.id">{{item.title + ' ' + item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from "lodash.pick";

export default {
  props: {
    title: {
      type: String,
      required: true
    },
    members: {
      type: Array,
      default: () => []
    },
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    return {
      form: this.$form.createForm(this),
      layout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 13 }
        }
      }
    }
  },
  created () {
    const f = ["id", "arrangedPlace", "date", "time", "members"]
    f.forEach(v => this.form.getFieldDecorator(v))
    this.$watch('model', () => {
      if (this.model) this.form.setFieldsValue(pick(this.model, f))
    })
    this.$watch('visible', () => {
      if (!this.model) this.form.resetFields()
    })
  },
  methods: {
    ok() {
      this.form.validateFields((errors, values) => {
        if (!errors) {
          this.$emit('ok', values)
        }
      })
    }
  }
}
</script>
